//
//  ComposeToolBar.m
//  CC
//
//  Created by Calvin on 5/20/15.
//  Copyright (c) 2015 Calvin. All rights reserved.
//

#import "ComposeToolBar.h"
#import "UIImage+Extension.h"

@implementation ComposeToolBar
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"compose_toolbar_background"]];
        [self addButtonWithIcon:@"compose_trendbutton_background" highIcon:@"compose_trendbutton_background_highlighted" tag:ComposeToolbarButtonTypeTrend];
        [self addButtonWithIcon:@"compose_camerabutton_background" highIcon:@"compose_camerabutton_background_highlighted"tag: ComposeToolbarButtonTypeCamera];
        [self addButtonWithIcon:@"compose_toolbar_picture" highIcon:@"compose_toolbar_picture_highlighted" tag:ComposeToolbarButtonTypePicture];
        [self addButtonWithIcon:@"compose_emoticonbutton_background" highIcon:@"compose_emoticonbutton_background_highlighted"tag: ComposeToolbarButtonTypeEmotion];
        [self addButtonWithIcon:@"compose_addbutton_background" highIcon:@"compose_addbutton_background_highlighted" tag:ComposeToolbarButtonTypeMention];
        
    }
    return self;
}

-(void)addButtonWithIcon:(NSString *)icon highIcon:(NSString *) highIcon tag:(ComposeToolbarButtonType)tag
{
    UIButton *button = [[UIButton alloc] init];
    button.tag=tag;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];
    [button setImage:[UIImage imageWithName:highIcon] forState:UIControlStateHighlighted];
    [self addSubview:button];
    
}

-(void)buttonClick:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(composeTool:didClickedButton:)]) {
        [self.delegate composeTool:self didClickedButton:button.tag];
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    NSInteger count=self.subviews.count;
    CGFloat buttonW=self.frame.size.width/count;
    CGFloat buttonH=self.frame.size.height;
    for (int i=0; i<count; i++) {
        UIButton *button=self.subviews[i];
        CGFloat x= i*buttonW;
        CGFloat y = 0;
        CGFloat h = buttonH;
        CGFloat w = buttonW;
        button.frame = CGRectMake(x, y, w, h);
    }
}
@end
